package com.guofen.service.shlj.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.pojo.ShljAbnormalGarbage;
import com.guofen.pojo.ShljDeodorizationtowerWaterChange1;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.guofen.service.shlj.WaterMeterService;
import com.guofen.dao.ShljWaterMeterMapper;
import com.guofen.pojo.ShljWaterMeter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WaterMeterServiceImpl implements WaterMeterService{
    @Autowired
    private ShljWaterMeterMapper shljWaterMeterMapper;

    @Override
    public boolean add(Double wMeterReading,
                       String readingPerson,
                       String remarks){
        Date now = new Date();
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
        String date = DateAndTimeUtil.getDate();
        String wreadtime = timeFormat.format(now);

        ShljWaterMeter shljWaterMeter = new ShljWaterMeter();
        shljWaterMeter.setDate(date);
        shljWaterMeter.setwReadingTime(wreadtime);
        shljWaterMeter.setwMeterReading(wMeterReading);
        shljWaterMeter.setReadingPerson(readingPerson);
        shljWaterMeter.setRemarks(remarks);

        int result = shljWaterMeterMapper.insert(shljWaterMeter);
        if (result == 1)
            return true;
        else
            return false;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<ShljWaterMeter> list = shljWaterMeterMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<ShljWaterMeter> list = shljWaterMeterMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return shljWaterMeterMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(ShljWaterMeter record) {
        return shljWaterMeterMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public ShljWaterMeter getlast(){
        return shljWaterMeterMapper.getLast();
    }
}